/*
 * Copyright 2011 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.swf.converter
{
	import flash.geom.ColorTransform;
	import sweezy.swf.structure.CXForm;
	import sweezy.swf.structure.CXFormWithAlpha;

	public class ColorTransformConverter
	{

		public static function alphaColorTransformNativeToStructure(from:ColorTransform):CXFormWithAlpha
		{
			if (from === null)
			{
				return null;
			}

			var to:CXFormWithAlpha = new CXFormWithAlpha();
			to.redMultTerm = from.redMultiplier * 256;
			to.redAddTerm = from.redOffset;
			to.greenMultTerm = from.greenMultiplier * 256;
			to.greenAddTerm = from.greenOffset;
			to.blueMultTerm = from.blueMultiplier * 256;
			to.blueAddTerm = from.blueOffset;
			to.alphaMultTerm = from.alphaMultiplier * 256;
			to.alphaAddTerm = from.alphaOffset;

			return to;
		}

		public static function alphaColorTransformStructureToNative(from:CXFormWithAlpha):ColorTransform
		{
			if (from === null)
			{
				return null;
			}

			var to:ColorTransform = new ColorTransform();
			to.redMultiplier = from.redMultTerm / 256;
			to.redOffset = from.redAddTerm;
			to.greenMultiplier = from.greenMultTerm / 256;
			to.greenOffset = from.greenAddTerm;
			to.blueMultiplier = from.blueMultTerm / 256;
			to.blueOffset = from.blueAddTerm;
			to.alphaMultiplier = from.alphaMultTerm / 256;
			to.alphaOffset = from.alphaAddTerm;

			return to;
		}

		public static function colorTransformNativeToStructure(from:ColorTransform):CXForm
		{
			if (from === null)
			{
				return null;
			}

			var to:CXForm = new CXForm();
			to.redMultTerm = from.redMultiplier * 256;
			to.redAddTerm = from.redOffset;
			to.greenMultTerm = from.greenMultiplier * 256;
			to.greenAddTerm = from.greenOffset;
			to.blueMultTerm = from.blueMultiplier * 256;
			to.blueAddTerm = from.blueOffset;

			return to;
		}

		public static function colorTransformStructureToNative(from:CXForm):ColorTransform
		{
			if (from === null)
			{
				return null;
			}

			var to:ColorTransform = new ColorTransform();
			to.redMultiplier = from.redMultTerm / 256;
			to.redOffset = from.redAddTerm;
			to.greenMultiplier = from.greenMultTerm / 256;
			to.greenOffset = from.greenAddTerm;
			to.blueMultiplier = from.blueMultTerm / 256;
			to.blueOffset = from.blueAddTerm;

			return to;
		}
	}
}
